/**
 * @name 辅助节点
 * @des 用于交互操作，比如添加节点
 */
import { Component, Input, OnInit } from "@angular/core";
import { ProcessService } from "../../../../server/process.service";
import { Pnode } from "src/app/@types/pnode";
import { NodeFactoryService } from "./../../../../server/node-factory.service";

@Component({
    selector: "helper-pnode",
    styles: [
        `
            :host .ico-wrapper::after {
                border-style: dashed;
            }
        `,
    ],
    template: `
        <div
            class="node-wrapper"
            (click)="addPnode()"
            nzTooltipTitle="添加节点"
            nzTooltipPlacement="top"
            nz-tooltip
        >
            <div class="ico-wrapper">
                <img
                    src="https://gitee.com/lyq2/blog-images/raw/master/imgs/pnode-add.png"
                    alt="ico"
                />
            </div>
        </div>

        <nz-modal
            [(nzVisible)]="isVisible"
            nzTitle="添加节点"
            (nzOnCancel)="handleCancel()"
            (nzOnOk)="handleOk()"
        >
            <ng-container *nzModalContent>
                <nz-tabset [(nzSelectedIndex)]="index">
                    <nz-tab nzTitle="动作节点">
                        <nz-radio-group [(ngModel)]="actionValue">
                            <label nz-radio [nzValue]="0">发送短信</label>
                            <label nz-radio [nzValue]="1">发送邮件</label>
                        </nz-radio-group>
                    </nz-tab>
                    <nz-tab nzTitle="条件节点">
                        <nz-radio-group [(ngModel)]="conditionValue">
                            <label nz-radio [nzValue]="0">复制分支</label>
                        </nz-radio-group>
                    </nz-tab>
                    <nz-tab nzTitle="流程节点">
                        <nz-radio-group [(ngModel)]="processValue">
                            <label nz-radio [nzValue]="2">结束</label>
                        </nz-radio-group>
                    </nz-tab>
                </nz-tabset>
            </ng-container>
        </nz-modal>
    `,
})
export class HelperComponent implements OnInit {
    /** 节点数据 */
    @Input() data: Pnode;

    /** 选中的节点类型 */
    index = 0;

    isVisible = false;

    actionValue = 0;
    conditionValue = 0;
    processValue = 2;

    yLevel = 0;

    constructor(
        private pSrv: ProcessService,
        private nfSrv: NodeFactoryService
    ) {}

    ngOnInit(): void {}

    /** 增加节点 */
    addPnode() {
        this.isVisible = true;
    }
    handleCancel() {
        this.isVisible = false;
    }
    handleOk() {
        const { addNodes, removeNpdes } = this.switchNode();
        this.pSrv
            .move(this.yLevel)
            .insert(addNodes)
            .remove(removeNpdes)
            .updatePedges();

        this.isVisible = false;
    }

    switchNode() {
        const { id: actNodeId, x, y, pid: actNodePid } = this.data;
        const addNodes = [];
        const removeNpdes = [];
        this.yLevel = 0;
        switch (this.index) {
            // 动作节点
            case 0:
                const aPnode = this.nfSrv.createActionPnode(
                    x,
                    y,
                    actNodePid,
                    this.actionValue
                );
                const apPnode = this.nfSrv.createHelperPnode(
                    x + 1,
                    y,
                    aPnode.id
                );
                addNodes.push(aPnode, apPnode);
                break;
            // 条件节点
            case 1:
                // 添加条件节点会改变整个流程的x,y，所以可能会引起大批量的更新
                // 如何判断需要更新的节点，可以从x轴入手，小于该x轴的节点并且y不等于0的节点都需要更新检查

                const cPnoes = this.nfSrv.createConditionPnodes(
                    x,
                    y,
                    actNodePid,
                    this.actionValue
                );
                const cpPnode1 = this.nfSrv.createHelperPnode(
                    x + 2,
                    y + 1,
                    cPnoes[2].id
                );
                const cpPnode2 = this.nfSrv.createHelperPnode(
                    x + 2,
                    y,
                    cPnoes[1].id
                );

                addNodes.push(...cPnoes, cpPnode1, cpPnode2);

                this.yLevel = y + 1;

                break;

            // 流程节点
            case 2:
                const pPnode = this.nfSrv.createProcessPnode(
                    x,
                    y,
                    actNodePid,
                    this.processValue
                );
                addNodes.push(pPnode);

                break;

            default:
                break;
        }
        removeNpdes.push({ id: actNodeId });

        return {
            addNodes,
            removeNpdes,
        };
    }
}
